var style = getStyleSheet('words');
if (style != null) {
    addRule(style, '.wt', 'border-collapse:collapse;border: 1px solid #606060;width:90%;margin: 10px 10px 10px 10px;');
    
    addRule(style, 'table.wt > tr > th', 'font-size:80%;text-align:center;border-style:solid;border-width:1px;border-color:#606060;color:#202020;background-color: #e0e0e0;');
    addRule(style, 'table.wt > tr > td', 'font-size:80%;text-align:left;border-style:solid;border-width:1px;border-color:#606060;text-indent:10px;');
    addRule(style, '.delwb', 'text-align: center;font-size: 80%;cursor: pointer;background-color: #e0e0e0;border-width: 1px;border-color: #606060;border-style: solid;');
    addRule(style, '.inpcell', 'width:90%;text-indent:0px;');
    
}

document.getElementById('testw').onclick = function testw(){
    sendRequest('word', '', null);
}

var wordId;
function setWordId(id){
    wordId = id;
}

function getWordId(){
    return wordId;
}

function selectEditCell(event){
	
	setDebugMessage(this);
	
    var _this;
    
    if (typeof(event) != 'undefined') {
		if (typeof(event.keyCode) != 'undefined') {
			setDebugMessage(event.keyCode);
            if(event.keyCode != 13){
				return true;
			}
			_this = event.currentTarget.parentNode;
        }
        else {
            _this = this;
        }
    }
    else {
        _this = this;
    }
    
    var cellid = _this.getAttributeNode("id");
    if (isNull(cellid)) {
        return false;
    }
    
    var inps = _this.getElementsByTagName('input');
    if (inps.length == 0) {
        var inp = document.createElement('input');
        setClass(inp, 'inpcell');
        inp.value = getText(_this, true);
        setText(_this, '');
		inp.setAttribute('type', 'text');
        inp.setAttribute('onkeypress','return selectEditCell1(event);');
        _this.appendChild(inp);
        
    }
    else {
        
        var ind = cellid.value.indexOf('_');
        if (ind == -1) {
            return false;
        }
        var id = cellid.value.substr(ind + 1, cellid.value.length - ind);
        var param = cellid.value.substr(0, ind);
        
        
        sendRequest('word', 'action=update&field=' + param + '&id=' + id + '&value=' + encodeURIComponent(inps[0].value), null);
        setText(_this, inps[0].value);
        _this.removeChild(inps[0]);
    }
}

function selectDelWordAction(){
    if (!isWait()) {
        var wordid = this.getAttributeNode("id");
        if (isNull(wordid)) {
            return false;
        }
        
        var ind = wordid.value.indexOf('_');
        if (ind == -1) {
            return false;
        }
        var id = wordid.value.substr(ind + 1, wordid.value.length - ind);
        setDebugMessage("del=" + id);
        var params = 'action=delete&id=' + id;
        setWordId(id);
        sendRequest('word', params, removeWordResponse);
        setWait();
    }
}

function removeWordResponse(){
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == '200') {
            var oldNote = document.getElementById('tr_' + getWordId());
            if (isNull(oldNote)) {
                return false;
            }
            oldNote.parentNode.removeChild(oldNote)
        }
        else {
            setDebugMessage('Response error: ' + httpRequest.status + ' ' + httpRequest.statusText);
        }
        cleanWait();
    }
}

function bodywords(){

    // initialize menu's items
    var table = document.getElementById("twid");
    if (table != null) {
        var childs = table.getElementsByTagName("div");
        if (isNull(childs)) {
            return false;
        }
        for (var i = 0; i < childs.length; i++) {
            var tagId = childs[i].getAttributeNode("id");
            if (isNull(tagId)) {
                return false;
            }
            if (tagId.value.substr(0, 4) == "bdw_") {
                childs[i].onclick = selectDelWordAction;
            }
        }
        var tds = table.getElementsByTagName("td");
        if (isNull(tds)) {
            return false;
        }
        for (var i = 0; i < tds.length; i++) {
            var tdId = tds[i].getAttributeNode("id");
            if (tdId != null) {
                tds[i].ondblclick = selectEditCell;
            }
        }
    }
};

bodywords();
